﻿using System.IO;
using System.Text;

public class FileSplitter
{
    private int fileIndex = 1;

    public void ConcatenateFiles(string firstFile, string concatenateFile)
    {
        FileStream outputStream = new FileStream(concatenateFile, FileMode.Create);
        using (outputStream)
        {
            string filePart = firstFile;
            for (int i = 0; i < 2; i++)
            {
                FileStream inputStream = new FileStream(filePart, FileMode.Open);
                using (inputStream)
                {
                    byte[] buffer = new byte[inputStream.Length];
                    inputStream.Read(buffer, 0, buffer.Length);
                    outputStream.Write(buffer, 0, buffer.Length);
                }
                filePart = filePart.Replace("001", "002");
            }
        }
    }

    public void SplitFile(string file)
    {
        FileStream inputStream = new FileStream(file, FileMode.Open);
        using (inputStream)
        {
            long partLength = inputStream.Length / 2 + 1;
            if (inputStream.Length % 2 == 0)
            {
                partLength = inputStream.Length / 2;
            }
            for (int i = 0; i < 2; i++)
            {
                long currentPartLength = partLength;
                long currentBufferSize = 4096;
                string filePartName = GenerateNextFileName(file);
                FileStream outputStream = new FileStream(filePartName,
                                                         FileMode.Create,
                                                         FileAccess.Write);
                using (outputStream)
                {
                    while (true)
                    {
                        if (currentBufferSize > currentPartLength)
                        {
                            currentBufferSize = currentPartLength;
                        }
                        else
                        {
                            currentPartLength = currentPartLength - currentBufferSize;
                        }
                        byte[] buffer = new byte[currentBufferSize];
                        int bytesRead = inputStream.Read(buffer, 0, buffer.Length);
                        if (bytesRead == 0)
                        {
                            break;
                        }
                        outputStream.Write(buffer, 0, buffer.Length);
                    }
                }
            }
        }
    }

    private string GenerateNextFileName(string file)
    {
        StringBuilder builder = new StringBuilder();
        int extensionIndex = file.LastIndexOf(".");
        string extension = file.Substring(extensionIndex);
        builder.Append(file.Substring(0, extensionIndex));
        builder.AppendFormat("-part{0:D3}", fileIndex);
        builder.Append(extension);
        fileIndex++;
        return builder.ToString();
    }
}